package org.redmagic.service.controller.ui;

import java.util.List;

import org.apache.log4j.BasicConfigurator;
import org.redmagic.domain.Property;
import org.redmagic.service.dao.MagicDao;
import org.redmagic.service.domain.ui.DataTablesRequest;
import org.redmagic.service.domain.ui.DataTablesResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;

@Controller
public class DanTestController {
	
	public DanTestController() {		
		BasicConfigurator.configure(); // Logging while testing ui
	}
	
	@Autowired
	private MagicDao magicDao;
	
	@RequestMapping(value = "/dan", method = RequestMethod.GET)
	public String viewAll() {
		return "dan";
	}
	
	@RequestMapping(value="/tags", method=RequestMethod.GET)
	public String viewTags() {
		return "tags";
	}
	
	@RequestMapping(value="/stats", method=RequestMethod.GET)
	public String viewStats() {
		return "stats";
	}
	
	@RequestMapping(value="/history", method=RequestMethod.GET)
	public String viewHistory() {
		return "history";
	}
	
	@RequestMapping(value="/ajax", method=RequestMethod.POST, consumes="application/json", produces="application/json")
	public @ResponseBody DataTablesResponse<List<String>> getData(DataTablesRequest request) {
		
		List<Property> properties = magicDao.getAllProperties();
		
		DataTablesResponse<List<String>> response = new DataTablesResponse<List<String>>();
		response.echo = 1;
		response.totalDisplayRecords = properties.size();
		response.totalRecords = properties.size();
		
		for (Property property : properties) {
			
			response.data.add(Lists.newArrayList(property.getName(),
												 property.getTags().toString(),
												 property.getValue(),
												 "123",
												 "midnight",
												 "dan"));
		}

		return response;
    }
	
	
}